package com.lightboat.common.utils;

import com.lightboat.common.constant.StringPool;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

public class SortParamConverter {
    
    public static List<SortField> convertSortParams(String input) {
        List<SortField> result = new ArrayList<>();
        String[] parts = input.split(StringPool.COMMA);
        
        for (String part : parts) {
            part = part.trim();
            String[] fieldAndOrder = part.split("\\s+");
            
            String field = fieldAndOrder[0].trim();
            String order = "asc"; // 默认升序
            
            if (fieldAndOrder.length > 1) {
                order = fieldAndOrder[1].trim().toLowerCase();
                if (!order.equals("desc")) {
                    order = "asc";
                }
            }
            
            result.add(new SortField(field, order));
        }
        
        return result;
    }

    /**
     * @param field Getters
     */
    public record SortField(String field, String order) {

    @Override
        public String toString() {
            return field + StringPool.COMMA + order;
        }

    }

    public static void main(String[] args) {
        // 测试用例
        String[] testCases = {
            "order_time desc,customer_id desc",
            "order_time,customer_id desc",
            "order_time,customer_id",
            "order_time,customer_id"
        };
        
        for (String testCase : testCases) {
            System.out.println("输入: " + testCase);
            List<SortField> converted = convertSortParams(testCase);
            System.out.print("输出: ");
            for (int i = 0; i < converted.size(); i++) {
                if (i > 0) System.out.print("；");
                System.out.print(converted.get(i));
            }
            System.out.println("\n");
        }
    }
}
